package com.ruoyi.purchase.service.impl;

import com.ruoyi.purchase.domain.AllocationDetail;
import com.ruoyi.purchase.domain.OrderDetail;
import com.ruoyi.purchase.domain.RawProductDetail;
import com.ruoyi.purchase.domain.SupplyDetail;
import com.ruoyi.purchase.mapper.AllocationDetailMapper;
import com.ruoyi.purchase.mapper.OrderDetailMapper;
import com.ruoyi.purchase.mapper.RawProductDetailMapper;
import com.ruoyi.purchase.mapper.SupplyDetailMapper;
import com.ruoyi.purchase.service.IAllocationDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * AllocationDetailService业务层处理
 * 
 * @author kevinness
 * @date 2025-01-18
 */
@Service
public class AllocationDetailService implements IAllocationDetailService
{
    @Autowired
    private AllocationDetailMapper allocationDetailMapper;

    @Autowired
    private RawProductDetailMapper rawProductDetailMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private SupplyDetailMapper supplyDetailMapper;

    /**
     * 查询AllocationDetail
     *
     * @param allocationNo AllocationDetail主键
     * @return AllocationDetail
     */
    @Override
    public List<AllocationDetail> selectAllocationDetailByAllocationNo(String allocationNo)
    {
        return allocationDetailMapper.selectAllocationDetailListByAllocationNo(allocationNo);
    }

    /**
     * 查询AllocationDetail列表
     *
     * @param allocationDetail AllocationDetail
     * @return AllocationDetail
     */
    @Override
    public List<AllocationDetail> selectAllocationDetailList(AllocationDetail allocationDetail)
    {
        return allocationDetailMapper.selectAllocationDetailList(allocationDetail);
    }

    /**
     * 新增AllocationDetail
     *
     * @param allocationDetail AllocationDetail
     * @return 结果
     */
    @Override
    public int insertAllocationDetail(AllocationDetail allocationDetail)
    {
        return allocationDetailMapper.insertAllocationDetail(allocationDetail);
    }

    /**
     * 新增调拨详情列表
     *
     * @param allocationDetailList
     * @return 结果
     */
    @Override
    public int insertAllocationDetailList(List<AllocationDetail> allocationDetailList){
        int result = 1;
        for (AllocationDetail allocationDetail : allocationDetailList){
            //设置货源地
            SupplyDetail supplyDetail = supplyDetailMapper.selectSupplyDetailByRawProductBatch(allocationDetail.getRawProductBatch());
            if(supplyDetail!=null){
                allocationDetail.setOrigin(supplyDetail.getOrigin());
            }
            result = insertAllocationDetail(allocationDetail);
            OrderDetail orderDetail = orderDetailMapper.selectOrderDetailByRawProductBatch(allocationDetail.getRawProductBatch());
            orderDetail.setAllocationQuantity(orderDetail.getAllocationQuantity() + allocationDetail.getWeight());
            orderDetailMapper.updateOrderDetail(orderDetail);
            if(result < 1){
                break;
            }
        }
        return result;
    }

    /**
     * 修改AllocationDetail
     *
     * @param allocationDetail AllocationDetail
     * @return 结果
     */
    @Override
    public int updateAllocationDetail(AllocationDetail allocationDetail)
    {
        return allocationDetailMapper.updateAllocationDetail(allocationDetail);
    }

    /**
     * 批量删除AllocationDetail
     *
     * @param allocationNos 需要删除的AllocationDetail主键
     * @return 结果
     */
    @Override
    public int deleteAllocationDetailByAllocationNos(String[] allocationNos)
    {
        return allocationDetailMapper.deleteAllocationDetailByAllocationNos(allocationNos);
    }

    /**
     * 删除AllocationDetail信息
     *
     * @param allocationNo AllocationDetail主键
     * @return 结果
     */
    @Override
    public int deleteAllocationDetailByAllocationNo(String allocationNo)
    {
        return allocationDetailMapper.deleteAllocationDetailByAllocationNo(allocationNo);
    }
}
